import COS from '@/static/js/COS.js';
const install = (Vue, vm) => {
	const getMedieUrl = (url) => {
		if (!url) {
			return require("@/static/images/lodding-err.png")
		} else {
			// 如果有https 字段不拼接 url
			if (url.indexOf('https') !== -1) {
				return url
			} else {
				return 'https://'+ process.uniEnv.COSHost+'/' + url //OSS 缩略图规则
			}
		}
	}
	/**
	 * @param {Object} =  [file] // file二进制对象
	 * @param {String} = [model] //文件类型
	 * @param {Number}  = [count] //文件个数
	 */
	const fileUpload = (model = 'image', count = 1, cbProgress, file) => {
		const vuex_token = uni.getStorageSync('vuex_token')
		return new Promise((resolve, reject) => {
			if (vuex_token) {
				switch (model) {

					default:
						if (count == 1) {
							uni.chooseImage({
								count,
								// 选择文件
								success: (data) => {

									const {
										tempFilePaths
									} = data

									let filePath = data.tempFiles[0].path;
									let filename = filePath.substr(filePath.lastIndexOf('/') + 1);

									COS.uploadFileToTencentClound(filename, filePath,model).then((res) => {
										resolve({fileName:res.split(`${process.uniEnv.COSBucket}.cos.${process.uniEnv.COSRegion}.myqcloud.com/`)[1]})
									})
									// vm.uploadFileOss(tempFilePaths[0], model, resolve, reject)
								},


								fail: ({
									errMsg
								}) => {
									uni.showToast({
										title: errMsg,
										icon: "error"
									})
									reject(errMsg)
								}
							})
						} else {
							uni.chooseImage({
								count,
								// 选择文件
								success: (data) => {
									let imgArr = [];
									console.log(data)
									const files = data.tempFiles;
									// for (let i = 0; i < files.length; i++) {
									// 	let obj = new Object();
									// 	obj.name = 'files';
									// 	obj.uri = files[i].path;
									// 	imgArr.push(obj);
									// }
									// vm.uploadFileOss(imgArr, model,resolve,reject)
									//支持多图上传
									// vm.uploadFileOss(data.tempFilePaths, model, resolve, reject)

									const promises = files.map(async (item) => {
										let filePath = item.path;
										let filename = filePath.substr(filePath.lastIndexOf('/') + 1);
										let res = await COS.uploadFileToTencentClound(filename, filePath,model).then((res) => {
											imgArr.push({fileName:res.split(`${process.uniEnv.COSBucket}.cos.${process.uniEnv.COSRegion}.myqcloud.com/`)[1]})
										})
									});
									Promise.all(promises).then((res) => {
										console.log(res)
										resolve(imgArr);
									});
								},


								fail: ({
									errMsg
								}) => {
									uni.showToast({
										title: errMsg,
										icon: "error"
									})
									reject(errMsg)
								}
							})
						}
				}
			} else {
				reject("token not Exist")
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}
		})

	}
	/**
	 * 单个视频文件上传
	 * @param {localPath} =  [url] //本地文件路径
	 * @param {String} = [model] //文件类型
	 * @param {Number}  = [count] //文件个数
	 */
	const videoUpload = (localPath, model, cbProgress) => {
		const vuexToken = uni.getStorageSync('vuex_token');

		return new Promise((resolve, reject) => {
			if (vuexToken) {

				vm.uploadFileOss(localPath, model, resolve, reject)
			} else {
				reject("token not Exist")
				uni.reLaunch({
					url: '/pages/login/login'
				})
			}
		})
	}
	vm.$u.file = {
		getMedieUrl,
		fileUpload,
		videoUpload
	}
}

export default {
	install
}